#include "PortNames.h"

#ifdef __cplusplus
extern "C"
{
#endif
    const uint16_t pin_map_gpio[] = {
        GPIO_Pin_0,
        GPIO_Pin_1,
        GPIO_Pin_2,
        GPIO_Pin_3,
        GPIO_Pin_4,
        GPIO_Pin_5,
        GPIO_Pin_6,
        GPIO_Pin_7,
        #ifdef GPIO_Pin_8
        GPIO_Pin_8,
        #endif
        #ifdef GPIO_Pin_9
        GPIO_Pin_9,
        #endif
        #ifdef GPIO_Pin_10
        GPIO_Pin_10,
        #endif
        #ifdef GPIO_Pin_11
        GPIO_Pin_11,
        #endif
        #ifdef GPIO_Pin_12
        GPIO_Pin_12,
        #endif
        #ifdef GPIO_Pin_13
        GPIO_Pin_13,
        #endif
        #ifdef GPIO_Pin_14
        GPIO_Pin_14,
        #endif
        #ifdef GPIO_Pin_15
        GPIO_Pin_15,
        #endif
    };

    GPIO_TypeDef *GPIOPort[MAX_NB_PORT] = {
        (GPIO_TypeDef *)GPIOA_BASE,
#ifdef GPIOB_BASE
        (GPIO_TypeDef *)GPIOB_BASE,
#endif
#ifdef GPIOC_BASE
        (GPIO_TypeDef *)GPIOC_BASE,
#endif
#ifdef GPIOD_BASE
        (GPIO_TypeDef *)GPIOD_BASE,
#endif
#ifdef GPIOE_BASE
        (GPIO_TypeDef *)GPIOE_BASE,
#endif
#ifdef GPIOF_BASE
        (GPIO_TypeDef *)GPIOF_BASE,
#endif
#ifdef GPIOG_BASE
        (GPIO_TypeDef *)GPIOG_BASE,
#endif
#ifdef GPIOH_BASE
        (GPIO_TypeDef *)GPIOH_BASE,
#endif
#ifdef GPIOI_BASE
        (GPIO_TypeDef *)GPIOI_BASE,
#endif
#ifdef GPIOJ_BASE
        (GPIO_TypeDef *)GPIOJ_BASE,
#endif
#ifdef GPIOK_BASE
        (GPIO_TypeDef *)GPIOK_BASE,
#endif
#ifdef GPIOZ_BASE
        (GPIO_TypeDef *)GPIOZ_BASE,
#endif
    };

    /* Enable GPIO clock and return GPIO base address */
    GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx)
    {
        GPIO_TypeDef *gpioPort = 0;
        switch (port_idx)
        {
        case PortA:
            gpioPort = (GPIO_TypeDef *)GPIOA_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
            break;
#if defined GPIOB_BASE
        case PortB:
            gpioPort = (GPIO_TypeDef *)GPIOB_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
            break;
#endif
#if defined GPIOC_BASE
        case PortC:
            gpioPort = (GPIO_TypeDef *)GPIOC_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
            break;
#endif
#if defined GPIOD_BASE
        case PortD:
            gpioPort = (GPIO_TypeDef *)GPIOD_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
            break;
#endif
#if defined GPIOE_BASE
        case PortE:
            gpioPort = (GPIO_TypeDef *)GPIOE_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
            break;
#endif
#if defined (GPIOF_BASE) && defined(RCC_APB2Periph_GPIOF)
        case PortF:
            gpioPort = (GPIO_TypeDef *)GPIOF_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
            break;
#endif
#if defined GPIOG_BASE && defined(RCC_APB2Periph_GPIOG)
        case PortG:
            gpioPort = (GPIO_TypeDef *)GPIOG_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);
            break;
#endif
#if defined GPIOH_BASE
        case PortH:
            gpioPort = (GPIO_TypeDef *)GPIOH_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOH, ENABLE);
            break;
#endif
#if defined GPIOI_BASE
        case PortBI:
            gpioPort = (GPIO_TypeDef *)GPIOI_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOI, ENABLE);
            break;
#endif
#if defined GPIOBJ_BASE
        case PortJ:
            gpioPort = (GPIO_TypeDef *)GPIOBJ_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOJ, ENABLE);
            break;
#endif
#if defined GPIOK_BASE
        case PortK:
            gpioPort = (GPIO_TypeDef *)GPIOK_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOK, ENABLE);
            break;
#endif
#if defined GPIOZ_BASE
        case PortZ:
            gpioPort = (GPIO_TypeDef *)GPIOZ_BASE;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOZ, ENABLE);
            break;
#endif
        default:
            break;
        }
        return gpioPort;
    }

#ifdef __cplusplus
}
#endif